package controller

import (
	"sharePicture/models/dto"
	"sharePicture/service"
	"strconv"

	"github.com/gin-gonic/gin"
)

// 上传图片
func UploadPicture(r *gin.Context) {

	uid, _ := strconv.Atoi(r.PostForm("uid"))
	s := r.PostFormArray("groups")
	groups := []int{}
	for i := 0; i < len(s); i++ {
		x, _ := strconv.Atoi(s[i])
		groups = append(groups, x)
	}
	art := r.PostForm("article")
	form, _ := r.MultipartForm()
	files := form.File["pictures"]
	picture, ok := service.UploadPicture(uid, groups, art, files, r)
	if ok {
		r.JSON(200, dto.MessageDto{}.Sucessful("成功", picture))
		return
	} else {
		r.JSON(200, dto.MessageDto{}.Error("失败"))
		return
	}
}

// 获取标签集合
func GetGroupsList(r *gin.Context) {
	list, ok := service.GetGroupsList()
	if ok {
		r.JSON(200, dto.MessageDto{}.Sucessful("成功", list))
		return
	} else {
		r.JSON(200, dto.MessageDto{}.Error("失败"))
		return
	}
}

// 查看照片详情
func GetPictureInfo(r *gin.Context) {
	uid, _ := strconv.Atoi(r.Query("uid"))
	pid, _ := strconv.Atoi(r.Query("pid"))
	pd, b := service.GetPictureInfo(uid, pid)
	if b {
		r.JSON(200, dto.MessageDto{}.Sucessful("成功", pd))
	} else {
		r.JSON(200, dto.MessageDto{}.Error("失败"))
	}
}
